package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.api.admin.dto.CompanyGoodsListRequest;
import com.jmp.api.admin.dto.CompanyGoodsListResponse;
import com.jmp.api.admin.dto.CompanyGoodsSpecUpdateRequest;
import com.jmp.api.admin.dto.CompanyGoodsUpdateRequest;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.GoodsModelUpdateRequest;
import com.jmp.base.dto.GoodsVrUpdateRequest;
import com.jmp.feign.GoodsFeign;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.GoodSpecMapperExt;
import one.stand.mapper.GoodsApiAdminMapper;
import one.stand.model.GoodSpecModel;
import one.stand.model.Goods;
import one.stand.model.GoodsType;
import one.stand.model.ResultModel;
import one.stand.service.GoodsPhotoService;
import one.stand.service.GoodsTypeService;
import one.stand.service.OrderInfoBaseService;
import one.stand.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by yandou on 2019/6/18.
 */
@RestController
@RequestMapping("adminApi/company/goods")
public class CompanyGoodsController extends BaseController {
    @Autowired
    private GoodsFeign goodsFeign;

    @Autowired
    private GoodsApiAdminMapper goodsApiAdminMapper;
    @Autowired
    private GoodSpecMapperExt goodSpecMapperExt;
    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private GoodsPhotoService goodsPhotoService;
    @Autowired
    private OrderInfoBaseService orderInfoBaseService;

    /**
     * 企业产品列表
     *
     * @return
     */
    @PostMapping("list")
    @SecurityAuthCheck
    public ResultModel<CompanyGoodsListResponse> list(@RequestBody CompanyGoodsListRequest request) {
        String nameLike = SqlUtil.like(request.getNameLike());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Goods> list = goodsApiAdminMapper.selectByCompany(request.getCompanyId(), nameLike, request.getCategoryId());

        PageResponse2<CompanyGoodsListResponse.Goods> goodsList = ConvertUtils.page2(list, goodsModel -> {
            GoodSpecModel goodSpecModel = new GoodSpecModel();
            goodSpecModel.setGoodsId(goodsModel.getGoodsId());
            List<GoodSpecModel> goodSpecModelList = goodSpecMapperExt.getSpec(goodSpecModel);

            GoodsType goodsType = goodsTypeService.get(goodsModel.getGoodstypeId());

            return CompanyGoodsListResponse.Goods.builder()
                    .goodSpecModelList(goodSpecModelList)
                    .goodsTypeName(goodsType.getName())
                    .goodPhoto(goodsPhotoService.getPath(goodsModel.getGoodsId()))
                    .dealNum(orderInfoBaseService.countSales(goodsModel.getGoodsId()))
                    .goodsId(goodsModel.getGoodsId())
                    .goodsName(goodsModel.getGoodsName())
                    .price(goodsModel.getPrice())
                    .qjWholesalePrice(goodsModel.getQjWholesalePrice())
                    .qjAgentTime(goodsModel.getQjAgentTime())
                    .qjVrUrl(goodsModel.getQjVrUrl())
                    .qjOrginId(goodsModel.getQjOrginId())
                    .build();
        });

        CompanyGoodsListResponse response = CompanyGoodsListResponse.builder()
                .goodsList(goodsList)
                .build();
        return ResultModel.success(response);
    }

    @PostMapping("update")
    @SecurityAuthCheck
    public ResultModel goodsUpdate(@RequestBody CompanyGoodsUpdateRequest request) {
        goodsFeign.vrUpdate(GoodsVrUpdateRequest.builder().goodsId(request.getGoodsId()).qjVrUrl(request.getQjVrUrl()).build());
        return ResultModel.success();
    }

    @PostMapping("spec/update")
    @SecurityAuthCheck
    public ResultModel updateSpec(@RequestBody CompanyGoodsSpecUpdateRequest request) {
        GoodsModelUpdateRequest modelUpdateRequest = GoodsModelUpdateRequest.builder()
                .specId(request.getSpecId())
                .modelPicUrl(request.getModelPicUrl())
                .modelUrl(request.getModelUrl())
                .modelZipPath(request.getModelZipPath())
                .build();
        goodsFeign.modelUpdate(modelUpdateRequest);
        return ResultModel.success();
    }
}
